/*
package com.shiyi.system.aop;

import com.shiyi.core.utils.result.Result;
import com.shiyi.core.vo.Menu;
import com.shiyi.core.vo.TokenUser;
import com.shiyi.system.annotation.NoPermission;
import com.shiyi.system.utils.UserManager;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.List;

*/
/**
 * @author shiyi
 * @version 1.0
 * @date 2020-12-8 16:3
 *
 *//*



@Aspect
@Component
public class PermissionAop {

    @Value("${server.port}")
    private Integer port;

    @Autowired
    private UserManager userManager;

    @Pointcut(("execution(public * com.shiyi.system.controller.*.*(..))"))
    public void logPoint() {}


    @Around("logPoint()")
    public Object doPermission(ProceedingJoinPoint joinPoint) throws Throwable {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert requestAttributes != null;
        HttpServletRequest request = requestAttributes.getRequest();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        // 有注解则放行
        NoPermission noPermission = method.getAnnotation(NoPermission.class);
        if (noPermission != null) {
            return joinPoint.proceed();
        }
        // 获取请求的url并转换成菜单中保存的格式
        String url = getRequestUrl(request);
        // 先拿到用户信息
        TokenUser userByToken = userManager.getUserByToken(request);
        if (url == null || userByToken == null) {
            return Result.NO_JURISDICTION;
        }
        List<Menu> menus = userByToken.getMenus();
        if (menus != null && menus.size() > 0) {
            for (Menu menu : menus) {
                if (url.equals(menu.getMenuUrl())) {
                    return joinPoint.proceed();
                }
            }
        }
        return Result.NO_JURISDICTION;
    }


    private String getRequestUrl(HttpServletRequest request) {
        StringBuilder builder = new StringBuilder();
        StringBuffer url = request.getRequestURL();
        int i = url.indexOf(String.valueOf(port));
        String uri = url.substring(i + 5);
        String[] split = uri.split("/");
        if (split.length >= 3) {
            for (int i1 = 0; i1 < 3; i1++) {
                builder.append(split[i1]).append(":");
            }
            return builder.substring(0, builder.length() - 1);
        }
        return null;
    }
}
*/
